package enumandlambda;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import static java.lang.Thread.MAX_PRIORITY;

/**
 * @author shkstart
 * 线程工厂的演示
 * @create 2022-12-07-21:02
 */
public class ThreadPoolDemo3 {
    public static void main(String[] args) {
        //创建线程工厂
        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                //这里一定要把任务Runnable 设置给新创建的线程
                Thread thread = new Thread(r);
                //设置线程的命名规则
                thread.setName("我的线程"+r.hashCode());
                //设置线程的优先级
                thread.setPriority(MAX_PRIORITY);
                return thread;

            }
        };
        ExecutorService service = Executors.newFixedThreadPool(5,factory);
        for (int i = 0; i < 5; i++) {
            service.submit(()->{
                //任务
                Thread thread = Thread.currentThread();
                System.out.println("线程池开始执行了" + thread.getName()+
                        "线程优先级"+thread.getPriority() );
            });
        }

    }
}
